/*
	Copyright 2012 J. Gohlke
	Licensed under the Apache License, Version 2.0 (the
	"License"); you may not use this file except in
	compliance with the License. You may obtain a copy
	of the License at

	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in
	writing, software distributed under the License is
	distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
	OR CONDITIONS OF ANY KIND, either express or
	implied. See the License for the specific language
	governing permissions and limitations under the
	License
 */
package com.gohlke.lwo.io;

import com.gohlke.lwo.types.BoundingBox;
import com.gohlke.lwo.types.Color;
import com.jme3.math.Vector3f;

import java.io.*;

/**
 *
 * @author David Wise
 */
public class LightwaveObjectInputStream extends DataInputStream {
 
    private final int tagLength = 4;

    /*
     * Stores a mark for later retrieval of bytes read since the mark
     */
    protected int offset = 0;
    
    public LightwaveObjectInputStream( byte[] buffer ) throws IOException, InvalidObjectException {
        super(new ByteArrayInputStream(buffer));
    }
    public LightwaveObjectInputStream( InputStream in ) throws IOException, InvalidObjectException {
        super(in);
    }
    
    
    public void setPositionMark() throws IOException{
        offset = this.available();
    }
    public int getReadFromMark() throws IOException {
        return offset - this.available();
    }
    
    /*
     * Reads a NULL terminated string, padded to an even length
     */
    public final String readString() throws java.io.IOException {
        String ret = "";
        int c;
        while ((c = super.read()) != '\0')
            ret += (char)c;
        if (ret.length() % 2 == 0)
            super.skipBytes(1);
        return ret;
    }

    // image specific
    public final Color readColor() throws java.io.IOException {
        return new Color(super.readFloat(), super.readFloat(), super.readFloat());
    }
    public final int readRGB() throws IOException {
        return (this.read() << 16) | (this.read() << 8) | this.read();
    }
    public final java.awt.image.BufferedImage readIcon(int len) throws IOException {
        int width = this.readUnsignedShort();
        int height = (len - 4) / width;
        java.awt.image.BufferedImage ret = new java.awt.image.BufferedImage
                (width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);
        for (int y = 0; y < height; y++)
            for (int x = 0; x < width; x++)
                ret.setRGB(x, y, readRGB());
        return ret;
    }
        
    // 3D specific
    public final Vector3f readVector3f() throws java.io.IOException {
        return new Vector3f(super.readFloat(), super.readFloat(), super.readFloat());
    }
    public final BoundingBox readBoundingBox() throws java.io.IOException {
        BoundingBox bbox = new BoundingBox();
        bbox.min = this.readVector3f();
        bbox.max = this.readVector3f();
        return bbox;
    }

    // LWO specific
    /*
     * Reads a tag (4 char identifier)
     * 
     * It's trimmed in order to convert it to an enum easily
     */
    public final String readTag() throws java.io.IOException {
        byte[] buffer = new byte[tagLength];
        super.read(buffer, 0, tagLength);
        return (new String(buffer)).trim();
    }

    /*
     * Read a variable length index (VX)
     */
    public final int readVX() throws java.io.IOException {
        int temp = this.read();
        if (temp != 0xFF)
            return (temp << 8) | this.read();
        else
            return (this.read()<<16) | (this.read()<<8) | this.read();
    }
    
    
}
